<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Web高级开发 | Advanced Web Development Course</title>
    <link
      href="https://fonts.googleapis.com/css?family=Heebo:300,400"
      rel="stylesheet"
    />
    <link rel="stylesheet" href="../../css/main.css" />
    <link rel="stylesheet" href="../../css/tabs.css" />
    <script src="../../js/uikit.js"></script>
    <link href="../../css/prism.css" rel="stylesheet" />
    <script src="../../js/prism.js"></script>
  </head>

  <body>
    <div
      data-uk-sticky="animation: uk-animation-slide-top; sel-target: .uk-navbar-container; cls-active: uk-navbar-sticky; cls-inactive: uk-navbar-transparent; top: 300"
    >
      <nav class="uk-navbar-container pt-2 pb-2">
        <div class="uk-container">
          <div data-uk-navbar>
            <div class="uk-navbar-left">
              <a
                class="uk-navbar-item uk-logo uk-visible@m"
                href="../../index.html"
                >Web高级开发课程</a
              >
              <a
                class="uk-navbar-tuk-containeroggle uk-hidden@m"
                href="#offcanvas-docs"
                data-uk-toggle
                ><span data-uk-navbar-toggle-icon></span>
                <span class="uk-margin-small-left">目录</span></a
              >
              <ul class="uk-navbar-nav uk-visible@m">
                <li><a href="../../index.html">文档</a></li>
              </ul>
            </div>
            <div class="uk-navbar-center uk-hidden@m">
              <a class="uk-navbar-item uk-logo" href="../../index.html"
                >Web高级开发课程</a
              >
            </div>
          </div>
        </div>
      </nav>
    </div>

    <div class="uk-section">
      <div class="uk-container">
        <div class="uk-grid-large" data-uk-grid>
          <div class="sidebar-fixed-width uk-visible@m">
            <div class="sidebar-docs uk-position-fixed uk-margin-top">
              <h5>关于这门课</h5>
              <ul class="uk-nav uk-nav-default doc-nav">
                <li><a href="../../index.html">大纲</a></li>
              </ul>
              <h5>章节</h5>
              <ul class="uk-nav uk-nav-default doc-nav">
                <li><a href="../01.html">第1章 - Spring基础</a></li>
                <li><a href="../02.html">第2章 - Spring中的Bean</a></li>
                <li><a href="../03.html">第3章 - Spring AOP</a></li>
                <li><a href="../04.html">第4章 - Spring的事物管理</a></li>
                <li><a href="../05.html">第5章 - 初识MyBatis</a></li>
                <li><a href="../06.html">第6章 - MyBatis的核心配置</a></li>
                <li><a href="../07.html">第7章 - MyBatis的高级应用</a></li>
                <li><a href="../08.html">第8章 - Spring MVC入门</a></li>
                <li><a href="../09.html">第9章 - Spring MVC数据绑定</a></li>
                <li><a href="../10.html">第10章 - Spring MVC拦截器</a></li>
                <li><a href="../11.html">第11章 - Spring Boot基础</a></li>
                <li><a href="../12.html">第12章 - Spring Boot的Web开发</a></li>
                <li>
                  <a href="../13.html">第13章 - Spring Boot的数据库连接</a>
                </li>
                <li><a href="../14.html">第14章 - 框架整合</a></li>
              </ul>
              <h5>实验课</h5>
              <ul class="uk-nav uk-nav-default doc-nav">
                <li><a href="./code-01.html">01</a></li>
                <li><a href="./code-02.html">02</a></li>
                <li><a href="./code-03.html">03</a></li>
                <li><a href="./code-04.html">04</a></li>
                <li><a href="./code-05.html">05</a></li>
                <li><a href="./code-06.html">06</a></li>
                <li><a href="./code-07.html">07</a></li>
                <li><a href="./code-08.html">08</a></li>
                <li><a href="./code-09.html">09</a></li>
                <li class="uk-active"><a href="./code-10.html">10</a></li>
                <li><a href="./code-11.html">11</a></li>
                <li><a href="./code-12.html">12</a></li>
              </ul>
            </div>
          </div>
          <div class="uk-width-1-1 uk-width-expand@m">
            <article class="uk-article">
              <h1>实验课10 - Spring Boot + Mybatis</h1>

              <div>
                <h3>1.新建项目</h3>
                <p>
                  File --&gt; New --&gt; Project --&gt; Spring Starter Project
                  --&gt; Name （随便起一个） --&gt; 再勾选 Mybatis Framework,
                  MySql Driver 和 Spring Web --&gt; Finish
                </p>
                <h3>2.基本结构</h3>
                <p>
                  <img src="../../images/lab10/structure.png" referrerpolicy="no-referrer" />
                </p>
                <p>数据库中的表tb_user，里面的数据有这些。</p>
                <p>
                  <img
                    src="../../images/lab10/tb_user.JPG"
                    referrerpolicy="no-referrer"
                    alt="tb_user"
                  />
                </p>
                <p style="color:red"><strong>注意！对于实验室M5-210的机器，请点击桌面上的SLQ Server Manager然后用下面的方式登录，并建立相应的数据库</strong></p>
                <img src="../../images/lab10/222222.JPG" alt="">
                <img src="../../images/lab10/111111.JPG" alt="">
                
                
                <h3>3.操作一波</h3>
                <p>User类</p>
                <pre><code class='language-java' lang='java'>package com.qust.pojo;
                
public class User {
  private Integer id;
  private String username;
  private String password;
  
  public Integer getId() {
    return id;
  }
  public void setId(Integer id) {
    this.id = id;
  }
  public String getUsername() {
    return username;
  }
  public void setUsername(String username) {
    this.username = username;
  }
  public String getPassword() {
    return password;
  }
  public void setPassword(String password) {
    this.password = password;
  }
  @Override
  public String toString() {
    return &quot;User [id=&quot; + id + &quot;, username=&quot; + username + &quot;, password=&quot; + password + &quot;]&quot;;
  }
}
</code></pre>
                <p>UserMapper Interface</p>
                <p>注意这里要用到注解 @Mapper 去告诉Springboot 这是个Mapper</p>
                <pre><code class='language-java' lang='java'>package com.qust.mapper;
                
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.qust.pojo.User;

@Mapper
public interface UserMapper {
  List&lt;User&gt; findAll();
  int addUser(User user);
  int updateUser(User user);
  int deleteUserById(Integer id);
}
</code></pre>
<p>
  UserMapper.xml (这个文件我是放在了
  src/main/resources/mybatis/mapper 文件夹内)
</p>
<pre><code class='language-xml' lang='xml'>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;!DOCTYPE mapper PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot; &quot;http://mybatis.org/dtd/mybatis-3-mapper.dtd&quot;&gt;
&lt;mapper namespace=&quot;com.qust.mapper.UserMapper&quot;&gt;
  &lt;select id=&quot;findAll&quot; resultType=&quot;com.qust.pojo.User&quot;&gt;
    SELECT * FROM tb_user
  &lt;/select&gt;
  &lt;insert id=&quot;addUser&quot;&gt;
    INSERT INTO tb_user(username, password) VALUES (#{username}, #{password})
  &lt;/insert&gt;
&lt;/mapper&gt;
</code></pre>
                <p>UserService interface:</p>
<pre><code class='language-java' lang='java'>package com.qust.service;

import java.util.List;
import com.qust.pojo.User;

public interface UserService {
  List&lt;User&gt; findAll();
  int addUser(User user);
  int updateUser(User user);
  int deleteUserById(Integer id);
}
</code></pre>
                <p>UserServiceImpl -- UserService的实现类</p>
                <p>
                  注意这里要用到注解 @Service(&quot;userService&quot;)
                  去告诉Springboot 这是个service组件
                </p>
<pre><code class='language-java' lang='java'>package com.qust.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qust.mapper.UserMapper;
import com.qust.pojo.User;
import com.qust.service.UserService;

@Service(&quot;userService&quot;)
public class UserServiceImpl implements UserService{

  @Autowired
  private UserMapper userMapper;
  
  @Override
  public List&lt;User&gt; findAll() {
    return userMapper.findAll();
  }

  @Override
  public int addUser(User user) {
    return userMapper.addUser(user);
  }

  @Override
  public int updateUser(User user) {
    // TODO Auto-generated method stub
    return 0;
  }

  @Override
  public int deleteUserById(Integer id) {
    // TODO Auto-generated method stub
    return 0;
  }
}
</code></pre>
<p>UserController类</p>
<p>
  注意这里要用到注解 @Controller 去告诉Springboot
  这是个controller组件
</p>
<pre><code class='language-java' lang='java'>package com.qust.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qust.pojo.User;
import com.qust.service.UserService;

@Controller
public class UserController {
  
  @Autowired
  UserService userService;
  
  @GetMapping(&quot;/findAll&quot;)
  @ResponseBody
  public List&lt;User&gt; findAll(){
    return userService.findAll();
  }
  
  @PostMapping(&quot;/addUser&quot;)
  @ResponseBody
  public int addUser(User user) {
    return userService.addUser(user);
  }
}
</code></pre>
                <h3>4.配置一波</h3>
                <p>
                  在src/main/resources/mybatis这个文件夹里新建一个文件，叫mybatis-config.xml
                </p>
                <p>
                  它的内容如下（其实没有啥实质性的内容，等下周再往里面加）：
                </p>
<pre><code class='language-xml' lang='xml'>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
&lt;!DOCTYPE configuration PUBLIC &quot;-//mybatis.org//DTD Config 3.0//EN&quot; &quot;http://mybatis.org/dtd/mybatis-3-config.dtd&quot;&gt;
&lt;configuration&gt;

&lt;/configuration&gt;
</code></pre>
                <p>
                  在src/main/resources/里的这个文件application.properties中，修改相应的代码
                </p>
                <p>
                  spring.datasource.username是你的数据库用户名，在这里我用的是root
                </p>
                <p>
                  spring.datasource.password是你的数据库密码，在这里我用的依然是root
                </p>
                <p>
                  我的数据库名字是springboot_mybatis
                </p>
                <p>最后再填入mybatis的配置文件：</p>
                <p>
                  mybatis.config-location=classpath:/mybatis/mybatis-config.xml
                  mybatis.mapper-locations=classpath:/mybatis/mapper/*.xml
                </p>
<pre><code class='language-xml' lang='xml'>spring.application.name=springboot-mybatis

server.port=8080

spring.resources.static-locations=classpath:/static/

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.name=defaultDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/springboot_mybatis?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root

mybatis.config-location=classpath:/mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:/mybatis/mapper/*.xml
</code></pre>
<p style="color:red"><strong>注意！对于实验室M5-210的机器，请用下面的配置内容</strong></p>
<pre><code class='language-xml' lang='xml'>spring.application.name=springboot-mybatis
server.port=8080

spring.resources.static-locations=classpath:/static/

spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.name=defaultDataSource
spring.datasource.url=jdbc:sqlserver://localhost:1433;integratedSecurity=true;databaseName=users

mybatis.config-location=classpath:/mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:/mybatis/mapper/*.xml
</code></pre>
<p style="color:red"><strong>其中这行代码 "spring.datasource.url=jdbc:sqlserver://localhost:1433;integratedSecurity=true;databaseName=users"
中的"databaseName=users"，users是你的数据库名称</strong></p>
                <h3>5.运行一波</h3>
                <p>
                  找到项目的主程序，比如我这里的主程序就是它：SpringbootMybatisApplication，然后运行它的main方法：
                </p>
                <p>(右键 --&gt; run as --&gt; java application)</p>
                <p>
                  <img src="../../images/lab10/main.JPG" referrerpolicy="no-referrer" />
                </p>
                <p>
                  用浏览器打开，发送findAll请求
                  <a
                    href="http://localhost:8080/findAll"
                    target="_blank"
                    class="url"
                    >http://localhost:8080/findAll</a
                  >
                  应该就能看到下面的内容了
                </p>
                <p>
                  <img
                    src="../../images/lab10/findAll.JPG"
                    referrerpolicy="no-referrer"
                  />
                </p>
                <p>
                  想操作添加用户，由于我定义了它是一个Post请求，而浏览器的地址栏都是Get请求，所以在浏览器的地址栏实现不了post操作，于是可以借助postman这个软件。
                </p>
                <p>
                  <img
                    src="../../images/lab10/postman1.JPG"
                    referrerpolicy="no-referrer"
                  />
                </p>
                <p>在这个软件中</p>
                <ol>
                  <li>选择post</li>
                  <li>
                    输入<a
                      href="http://localhost:8080/addUser"
                      target="_blank"
                      class="url"
                      >http://localhost:8080/addUser</a
                    >
                  </li>
                  <li>
                    选择body下的x-www-form-unlencoded，在输入username和password的值，点send
                  </li>
                  <li>
                    如果一切正常，最下面就会出现1这个数字，没有添加新用户成功的话，会显示0。<img
                      src="../../images/lab10/postman2.JPG"
                      referrerpolicy="no-referrer"
                      alt="postman2"
                    />
                  </li>
                </ol>
                <p>这时候wang5就会出现在数据库中了</p>
                <p>
                  <img
                    src="../../images/lab10/sss1.JPG"
                    referrerpolicy="no-referrer"
                    alt="sss"
                  />
                </p>
                <p>&nbsp;</p>
              </div>
            </article>
          </div>
        </div>
      </div>
    </div>

    <div id="offcanvas-docs" data-uk-offcanvas="overlay: true">
      <div class="uk-offcanvas-bar">
        <button class="uk-offcanvas-close" type="button" data-uk-close></button>
        <h5 class="uk-margin-top">关于这门课</h5>
        <ul class="uk-nav uk-nav-default doc-nav">
          <li><a href="../indext.html">大纲</a></li>
        </ul>
        <h5 class="uk-margin-top">章节</h5>
        <ul class="uk-nav uk-nav-default doc-nav">
          <li><a href="../01.html">第1章 - Spring基础</a></li>
          <li><a href="../02.html">第2章 - Spring中的Bean</a></li>
          <li><a href="../03.html">第3章 - Spring AOP</a></li>
          <li><a href="../04.html">第4章 - Spring的事物管理</a></li>
          <li><a href="../05.html">第5章 - 初识MyBatis</a></li>
          <li><a href="../06.html">第6章 - MyBatis的核心配置</a></li>
          <li><a href="../07.html">第7章 - MyBatis的高级应用</a></li>
          <li><a href="../08.html">第8章 - Spring MVC入门</a></li>
          <li><a href="../09.html">第9章 - Spring MVC数据绑定</a></li>
          <li><a href="../10.html">第10章 - Spring MVC拦截器</a></li>
          <li><a href="../11.html">第11章 - Spring Boot基础</a></li>
          <li><a href="../12.html">第12章 - Spring Boot的Web开发</a></li>
          <li><a href="../13.html">第13章 - Spring Boot的数据库连接</a></li>
          <li><a href="../14.html">第14章 - 框架整合</a></li>
        </ul>
        <h5>实验课</h5>
        <ul class="uk-nav uk-nav-default doc-nav">
          <li><a href="./code-01.html">01</a></li>
          <li><a href="./code-02.html">02</a></li>
          <li><a href="./code-03.html">03</a></li>
          <li><a href="./code-04.html">04</a></li>
          <li><a href="./code-05.html">05</a></li>
          <li><a href="./code-06.html">06</a></li>
          <li><a href="./code-07.html">07</a></li>
          <li><a href="./code-08.html">08</a></li>
          <li><a href="./code-09.html">09</a></li>
          <li class="uk-active"><a href="./code-10.html">10</a></li>
          <li><a href="./code-11.html">11</a></li>
          <li><a href="./code-12.html">12</a></li>
        </ul>
      </div>
    </div>

    <footer class="uk-section uk-text-center uk-text-muted">
      <div class="uk-container uk-container-small">
        <div class="uk-margin-medium"></div>
      </div>
    </footer>

    <script src="../js/awesomplete.js"></script>
    <script src="../js/custom.js"></script>
  </body>
</html>
